package com.wfh.activity;

import java.util.ArrayList;
import java.util.List;

import com.wfh.R;
import com.wfh.adapter.OrderAdapter;
import com.wfh.bean.ProductBean;
import com.wfh.util.MyDialog;
import com.wfh.util.Util;

import android.os.Bundle;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

public class OrderActivity extends CommonActivity {

	private MyDialog dialog;
	private ArrayList<ProductBean> list;
	private OrderAdapter adapter;
	private ListView listview;
	private Button pricedesc,order;
	private Button priceasc;
	private boolean mbundle = true;
	private BroadcastReceiver datareceiver;
	private Dialog alertDialog;
	private TextView content;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// Log.i("上下文",getApplicationContext().toString());
		super.onCreate(savedInstanceState, R.layout.activity_order);

		listview = (ListView) findViewById(R.id.listView1);
		pricedesc = (Button) findViewById(R.id.pricedesc);
		priceasc = (Button) findViewById(R.id.priceasc);
		order=(Button)findViewById(R.id.order);
		dialog = MyDialog.create(this);

		// 注册recevier广播接收器
		IntentFilter filter = new IntentFilter();
		filter.addAction(Util.D_ORDER);
		datareceiver = new DataReceiver();
		registerReceiver(datareceiver, filter);

		Intent intent = new Intent();
		intent.putExtra("order", "product_id");
		intent.putExtra("type", "desc");

		dialog.show();
		HomeActivity.service.getLocalOrder(this, intent);

		// 监听事件
		pricedesc.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!mbundle)
					return;
				Intent intent = new Intent();
				intent.putExtra("order", "product_price");
				intent.putExtra("type", "desc");
				HomeActivity.service.getLocalOrder(OrderActivity.this, intent);
				mbundle = false;
			}
		});

		priceasc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!mbundle)
					return;
				Intent intent = new Intent();
				intent.putExtra("order", "product_price");
				intent.putExtra("type", "asc");
				HomeActivity.service.getLocalOrder(OrderActivity.this, intent);
				mbundle = false;
			}
		});
		
		//点菜
		order.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				float total = 0;
				StringBuilder orderinfo=new StringBuilder();
				for(ProductBean bean:list){
					if(bean.getNumber()!=null && !bean.getNumber().equals("") && !bean.getNumber().equals("0")){//设置了份数，份数不为空，份数不为0
						float oneprice=Float.valueOf(bean.getProduct_price())*Float.valueOf(bean.getNumber());
						orderinfo.append(bean.getProduct_name()+"   份数:"+bean.getNumber()+"    金额:"+oneprice+"\n");
						total+=oneprice;
					}
				}
				orderinfo.append("总消费:"+total+"元");
				createDialog();
				content.setText(orderinfo.toString());
				alertDialog.show();
			}
		});

	}

	// 接收器
	private class DataReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bd = intent.getExtras();
			list = bd.getParcelableArrayList("products");
			if (list == null || list.size() == 0)
				return;
			if (adapter != null) {
				adapter.list = list;
				adapter.notifyDataSetChanged();
				// Util.setListViewHeightBasedOnChildren(listview);
				dialog.hide();
				mbundle = true;
				return;
			}
			adapter = new OrderAdapter(OrderActivity.this, list,listview);
			listview.setAdapter(adapter);
			// Util.setListViewHeightBasedOnChildren(listview);
			dialog.hide();
			mbundle = true;
		}
	}

	// 监听返回键
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.FIRST + 1, 1, "退出");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			android.os.Process.killProcess(android.os.Process.myPid());
			System.gc();
			System.exit(0);
			notify();
			break;
		default:
			break;
		}
		return true;
	}
	
	//创建带有滚动的dialog
	public void createDialog() {
		ScrollView scrollview = new ScrollView(this);
		scrollview.setBackgroundColor(Color.WHITE);
		content = new TextView(this);
		content.setTextColor(Color.BLACK);
		content.setPadding(10, 10, 10, 10);
		content.setWidth(400);
		scrollview.addView(content);
		alertDialog = Util.alertDialog(OrderActivity.this, scrollview, "点菜统计", "");

	}

	@Override
	protected void onDestroy() {
		if (dialog != null)
			dialog.cancel();
		unregisterReceiver(datareceiver);
		super.onDestroy();
	}

}
